/*
The MIT License

Copyright (c) 2010 Guillaume Florimond (www.valhalla.fr)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

package fr.valhalla.vcrypt.cyphers;

/**
 * Romanizer
 * Converts ARABIC numerals to ROMAN numerals (crypt())
 * and ROMAN numerals to ARABIC numerals (decrypt())
 *      Romanizer r = new Romanizer();
 *      r.setInput("12"); // injects 12 to convert
 *      r.crypt(); // converts 12 to XII
 *      String result = r.getOutput(); // returns the result
 * @author Guillaume Florimond < gf@valhalla.fr > < www.valhalla.fr >
 */
public class Romanizer extends Cypher {

    String[] roman = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; // cypher
    String[] roman_alt ={"CM", "CD", "XC", "XL", "IX", "IV", "M", "D", "C", "L", "X", "V", "I"}; // decypher
    Integer[] arab = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; // cypher
    Integer[] arab_alt = {900, 400, 90, 40, 9, 4, 1000, 500, 100, 50, 10, 5, 1}; // decypher

    public Romanizer() {
        super();
    }

    @Override
    public void crypt() {
        int tmp = new Integer(this.in).intValue();

        for(int i = 0 ; i < this.arab.length ; i++)
            tmp = divide(tmp, arab[i], i);
        
    }

    @Override
    public void decrypt() {
        String sub = this.in; // we'll work on this string
        for(int i = 0 ; i < roman_alt.length ; i++) // for each element in the roman_alt array
            // new string = old string with each occurrences of i item in roman_alt array replaced by i item in arab_alt array and a coma
            // e.g. XII => String("10,1,1,")
            sub = sub.replaceAll(roman_alt[i], new Integer(arab_alt[i]).toString() + ",");

        String[] sp = sub.split(","); // makes an array whith the sub string : String("10,1,1,") => String["10","1","1"]
        int rt = 0;
        for(int i = 0 ; i < sp.length ; i++) // for each element in the new sp array
            rt += new Integer(sp[i]).intValue(); // takes the integer value and adds it : ["10","1","1"] becomes 10+1+1 = 12

        this.out = new Integer(rt).toString();
    }

    /**
     * Divides num by denom to get the result and the rest (modulo).
     * Add the roman letter according to the result.
     * e.g. 300 / 100 = 3, do add 3 times 'C' : CCC
     * Method used by cypher()
     * @param num
     * @param denom
     * @param index
     * @return num % denom
     */
    private int divide(int num, int denom, int index) {
        int quo = (int) num / denom;
        int req = (int) num % denom; // modulo

        for(int i = 0 ; i < quo ; i++)
            this.out += this.roman[index];

        return req;
    }



}
